package com.qm.service.impl;

import com.alibaba.dubbo.common.json.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qm.service.SendSmsService;
import com.qm.util.ObjectMapperUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 @author james
 @create 2021-04-27 23:27
 用户登录名称 xiangyutao@1721981780634057.onaliyun.com
 AccessKey ID LTAI5tJ9dNBH4cZjgqRDtioJ
 AccessKey Secret KQfcZufDQBHbfpcFfZq2OIOa1v92sW

 */
@Service
public class SendSmsServiceImpl implements SendSmsService {
    @Override
    public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
//                "LTAI4FtxLPdzBMXjRf1kKYu7", "4iZnlU1qM5JoZd8CeB1tgDA5jgU5yg");
                "LTAI5tJ9dNBH4cZjgqRDtioJ", "KQfcZufDQBHbfpcFfZq2OIOa1v92sW");
        IAcsClient client = new DefaultAcsClient(profile);
        //构建请求
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //自定义的参数（手机号、验证码、签名、模板）
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "动吧旅游");//签名
        request.putQueryParameter("TemplateCode", templateCode);//模板
        request.putQueryParameter("TemplateParam", ObjectMapperUtil.toJSON(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
